<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class test_product extends CI_Controller {
    public function index(){
    	$this->load->model('test_product_model');
    	$products=$this->test_product_model->get_all();
    	$data=array(
    		'products'=>$products
    	);
    	$this->load->view('test_article', $data);
    }
  	public function add(){
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error help-block">', '</span>');
      	$this->form_validation->set_rules('title', '标题', 'required');
		$this->form_validation->set_rules('desc', '描述', 'required');
		$this->form_validation->set_rules('content', '内容', 'required|callback__content_check');
	    $this->load->model('test_product_model');
	    $product= new stdClass();
	    $product->title='';
	    $product->desc='';
	    $product->content='';
	    if ($this->form_validation->run() == FALSE){
	    	$data=array(
	    		'product'=>$product,
	    		'a'=>'添加'
	    	);
	    	$this->load->view('/article_form',$data);
	    }else{
	    	$data=$this->input->post();
	    	$this->test_product_model->add($data);
			redirect("/test_product/index");
	    }
  	}
  	public function edit($id){
  		$this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error help-block">', '</span>');
      	$this->form_validation->set_rules('title', '标题', 'required|max_length[20]');
		$this->form_validation->set_rules('desc', '描述', 'required');
		$this->form_validation->set_rules('content', '内容', 'required|callback__content_check');
	    $this->load->model('test_product_model');
	   	$product=$this->test_product_model->get_one($id);
	    if ($this->form_validation->run() == FALSE){
	    	$data=array(
	    		'product'=>$product,
	    		'a'=>'编辑'
	    	);
	    	$this->load->view('article_form',$data);
	    }else{
	    	$data=array(
	    		'product'=>$product,
	    		'a'=>'编辑'
	    	);
	    	$this->load->view('article_form',$data);
	    	$array=$this->input->post();
	    	$this->test_product_model->edit($array,$id);
			redirect("/test_product/index");
	    }
  	}
  	public function del($id){
  		$this->load->model('test_product_model');
  		$this->test_product_model->del($id);
  		redirect('/test_product/index');
  	}
   	public function _content_check(){
   		$this->load->model('test_product_model');
   		$content="%法轮功%";
   		$str=$this->test_product_model->reasch($content);
   		if($str){
			return true;
		}else{
			$this->form_validation->set_message('_content_check', '内容含有非法字符.');
			return FALSE;
		}
   	}
}
